import * as _ from "lodash"
import { camelCase } from "../case"
import { PageList } from "../data"
import { Author } from "../discuss"
import httpClient from "../httpclient"

export class OrderReview {
  public id: string
  public orderId: string
  public itemId: string
  public productId: string
  public variantId: string
  public author: Author
  public name: string
  public imageUrl: string
  public itemName: string
  public rating: number
  public body: string
  public bodyType: string
  public imageUrls: string[] = []
}


export class OrderReviewQuery {

  private params = {}

  public page(page?: string | number): OrderReviewQuery {
    if (page) {
      this.params["page"] = page
    }
    return this
  }

  public limit(limit?: string | number): OrderReviewQuery {
    if (limit) {
      this.params["limit"] = limit
    }
    return this
  }

  public storeId(storeId: string): OrderReviewQuery {
    this.params["store_id"] = storeId
    return this
  }

  public customerId(customerId: string): OrderReviewQuery {
    this.params["customer_id"] = customerId
    return this
  }

  public orderId(orderId: string): OrderReviewQuery {
    this.params["order_id"] = orderId
    return this
  }

  public list(): Promise<PageList<OrderReview>> {
    return httpClient.get("/v1/orders/reviews", {
      params: this.params,
    }).then(({ data }) => {
      return _.assign(new PageList<OrderReview>(), data, {
        elements: _.map(data.elements, review => _.assign(new OrderReview(), camelCase(review))),
      })
    })
  }

  public count(): Promise<number> {
    return httpClient.get("/v1/orders/reviews/count", {
      params: this.params,
    }).then(({ data }) => data)
  }
}

export class OrderReviewServiceV1 {
  public static createOrderReviewQuery() {
    return new OrderReviewQuery()
  }
}
